/*******************************************************************************
*  Project      : Minigate Golfin                                 
*  Program ID   : nicepay_billkey_result.js                                 
*  Description  : 결제
*
********************************************************************************
*  Program History                                                            
*  Date        Author    Description                                          
*  ----------  --------  -------------------------------------------------------- 
*  2012-05-21  김효순    Created.  
*******************************************************************************/

jQuery(function($){
	if (navigator.userAgent.match(/(iPhone|iPod|iPad)|(Android)/i)) {
    		if(_util.getParam("q_mobile") == "Y"){
    			document.objControl = function(){}
    			return;
    		}
    		
    		var lk = document.location.pathname;
    		lk += "?q_mobile=Y";
    		lk += "&resultCode=" + _resultCode;
    		lk += "&resultMsg=" + _resultMsg;
    		lk += "&GoodsName=" + _GoodsName;
    		lk += "&billkey=" + _billkey;
    		lk += "&cardCode=" + _cardCode;
    		lk += "&cardName=" + _cardName;
    		lk += "&MallReserved=" + _MallReserved;
    		
    		document.location.href = lk;
    		
    		return;
	}
	
	
	
	function clsComm()
	{
		
		this._init();
	}
	
	clsComm.prototype = {
		_init: function(){

		}
		// 보증예약
		, nicepaySubmit: function(arg){
			var thisClass = this;
			
			arg = arg || {};
			arg["콜백"] = arg["콜백"] || function(){}
			
			//ajax
			var serviceName = "reservAccess";
			var options = {
				"reserv_status": "G"
				, "reserv_seq": arg["예약코드"]
				, "resultCode": arg["결과코드"]
				, "resultMsg": arg["메시지"]
				, "goodsName": arg["상품명"]
				, "billkey": arg["빌키"]
				, "cardCode": arg["카드코드"]
				, "cardName": arg["카드사"]
				, "mid": arg["상점아이디"]
				, "mkey": arg["상점키"]
			}
			_gf.ajax({
				"sid": "RS001"
				, "servicename": serviceName
			}, options, function(ret){
				var res = {
					"결과코드": ret["msp"]["contents"]["serviceResult"]
					, "메시지": document.objGlobal.getResultMsg(ret)
				}
				
				arg["콜백"].apply(null, [res]);
			});
		}
	}
	
	// 컨트롤
	function clsControl()
	{
		this.objComm = null;
		
		this.q_vars = null;
		
		this._permitStart = null;
	}
	
	clsControl.prototype = {
		_init: function(){
			if (navigator.userAgent.match(/(iPhone|iPod|iPad)|(Android)/i)) {
				alert("asdfasdf");
			}
			
			
			
			this._preload();
			
			this.objComm = new clsComm();
			
			this._beforeStart();
			this._bind();
		}
		, _preload: function(){
			var mr = _MallReserved;
			var amr = mr.split("@!@!");
			this.q_vars = {}
			
			for(i=0;i<amr.length;i++){
				var t = amr[i].split("!@!@");
				this.q_vars[t[0]] = t[1];
			}
			
			if(opener){
				if(opener.document.objControl){
					this.openerControl = opener.document.objControl;
				}
			}
			
//			if(!this.openerControl){
//				self.close();
//				return;
//			}
		}
		, _beforeStart: function(){
			var thisClass = this;
			this._permitStart = {
				
			}
			
			_util.executeThread(function(){
				for(var k in thisClass._permitStart){
					if(!thisClass._permitStart[k]) return false;
				}
				
				return true;
			}, function(){
				_gf.spinEnd();
				thisClass._start();
			});
		}
		, _bind: function(){
			var thisClass = this;
			
			
		}
		, _start: function(){
			var thisClass = this;
			
			//document.charset = "euc-kr";
			
			if(_resultCode != "F100"){
				alert(_resultMsg);
				self.close();
				return;
			}
			
			_gf.spinStart("sending...");
			this.objComm.nicepaySubmit({
				"예약코드": this.q_vars["q_reserve"]
				, "결과코드": _resultCode
				, "메시지": _resultMsg
				, "상품명": _goodsName
				, "빌키": _billkey
				, "카드코드": _cardCode
				, "카드사": _cardName
				, "상점아이디": this.q_vars["mid"]
				, "상점키": this.q_vars["mkey"]
				, "콜백": function(ret){
					thisClass.finishNicepaySubmit(ret);
				}
			});
		}
		// 결제요청 완료
		, finishNicepaySubmit: function(ret){
//			if(ret["메시지"]) alert(ret["메시지"]);
//			if(ret["결과코드"] != "00000") return;

			this.openerControl.closeNicepay(self, ret);
			self.close();
		}
	}
	
	document.objControl = new clsControl();
	document.objControl._init();
});